{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\ProgramData\\Anaconda3\\lib\\site-packages\\linearmodels\\panel\\data.py:10: FutureWarning: The Panel class is removed from pandas. Accessing it from the top-level namespace will also be removed in the next version\n",
      "  from pandas import (Categorical, DataFrame, Index, MultiIndex, Panel, Series,\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd             # data package\n",
    "import matplotlib.pyplot as plt # graphics \n",
    "import datetime as dt\n",
    "import numpy as np\n",
    "\n",
    "import requests, io             # internet and input tools  \n",
    "import zipfile as zf            # zip file tools \n",
    "import os  \n",
    "\n",
    "from numpy.polynomial.polynomial import polyfit\n",
    "\n",
    "#import weightedcalcs as wc\n",
    "#import numpy as np\n",
    "\n",
    "import pyarrow as pa\n",
    "import pyarrow.parquet as pq\n",
    "\n",
    "import statsmodels.api as sm\n",
    "#import statsmodels\n",
    "#import statsmodels.formula.api as smf\n",
    "from linearmodels.iv import IV2SLS\n",
    "from linearmodels.panel import PanelOLS"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### \"Is the retaliation directed towards counties that vote for President Trump?\" \n",
    "\n",
    "Since the posting of the [paper](https://www.nber.org/papers/w26353), I have been asked this question many times:\n",
    "\n",
    "\"Is the retaliation directed towards counties that vote for President Trump?\"\n",
    "\n",
    "Eyeballing the map in Figure 1 of the paper suggest this is the case. Below I followed up with some basic regressions to explore this. Short answer is yes, but most the variation is about places that export the most to china. The interesting observation is that there is a strong correlation between those that exported a lot to china and the propensity to vote for President Trump.\n",
    "\n",
    "**Note I** This notebook is rough and prelimnary. Treat it as such. \n",
    "\n",
    "**Note II** The political economy aspect of the trade war is not what I'm interested. These results might end up in some footnote, but they are not a key part of the paper. Plus they are consistent with the [Fajgelbaum, Goldberg, Kennedy, and Khandelwal (2019)](https://www.nber.org/papers/w25638)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "file_path = os.getcwd()\n",
    "\n",
    "url = \"https://github.com/mwaugh0328/consumption_and_tradewar/raw/master/data/countypres_2000-2016.csv\"\n",
    "\n",
    "df = pd.read_csv(url, dtype = {\"FIPS\": str})\n",
    "\n",
    "# The data are from here: https://electionlab.mit.edu/data I can't figure out how to create a direct link, so \n",
    "# I will just post the data in the respository. \n",
    "\n",
    "df = df[df.year == 2016]\n",
    "# Just look at Trump election (?) does it correlate with other republican canidates?\n",
    "\n",
    "df[\"area_fips\"] = df.FIPS.astype(str)\n",
    "\n",
    "df = df[~df.FIPS.isna()]\n",
    "# Some places with no fips (look like write in counts)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>year</th>\n",
       "      <th>state</th>\n",
       "      <th>state_po</th>\n",
       "      <th>county</th>\n",
       "      <th>FIPS</th>\n",
       "      <th>office</th>\n",
       "      <th>candidate</th>\n",
       "      <th>party</th>\n",
       "      <th>candidatevotes</th>\n",
       "      <th>totalvotes</th>\n",
       "      <th>version</th>\n",
       "      <th>area_fips</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>41050</td>\n",
       "      <td>2016</td>\n",
       "      <td>Alabama</td>\n",
       "      <td>AL</td>\n",
       "      <td>Autauga</td>\n",
       "      <td>1001</td>\n",
       "      <td>President</td>\n",
       "      <td>Hillary Clinton</td>\n",
       "      <td>democrat</td>\n",
       "      <td>5936.0</td>\n",
       "      <td>24973</td>\n",
       "      <td>20190722</td>\n",
       "      <td>1001</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>41051</td>\n",
       "      <td>2016</td>\n",
       "      <td>Alabama</td>\n",
       "      <td>AL</td>\n",
       "      <td>Autauga</td>\n",
       "      <td>1001</td>\n",
       "      <td>President</td>\n",
       "      <td>Donald Trump</td>\n",
       "      <td>republican</td>\n",
       "      <td>18172.0</td>\n",
       "      <td>24973</td>\n",
       "      <td>20190722</td>\n",
       "      <td>1001</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>41052</td>\n",
       "      <td>2016</td>\n",
       "      <td>Alabama</td>\n",
       "      <td>AL</td>\n",
       "      <td>Autauga</td>\n",
       "      <td>1001</td>\n",
       "      <td>President</td>\n",
       "      <td>Other</td>\n",
       "      <td>NaN</td>\n",
       "      <td>865.0</td>\n",
       "      <td>24973</td>\n",
       "      <td>20190722</td>\n",
       "      <td>1001</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>41053</td>\n",
       "      <td>2016</td>\n",
       "      <td>Alabama</td>\n",
       "      <td>AL</td>\n",
       "      <td>Baldwin</td>\n",
       "      <td>1003</td>\n",
       "      <td>President</td>\n",
       "      <td>Hillary Clinton</td>\n",
       "      <td>democrat</td>\n",
       "      <td>18458.0</td>\n",
       "      <td>95215</td>\n",
       "      <td>20190722</td>\n",
       "      <td>1003</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>41054</td>\n",
       "      <td>2016</td>\n",
       "      <td>Alabama</td>\n",
       "      <td>AL</td>\n",
       "      <td>Baldwin</td>\n",
       "      <td>1003</td>\n",
       "      <td>President</td>\n",
       "      <td>Donald Trump</td>\n",
       "      <td>republican</td>\n",
       "      <td>72883.0</td>\n",
       "      <td>95215</td>\n",
       "      <td>20190722</td>\n",
       "      <td>1003</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       year    state state_po   county  FIPS     office        candidate  \\\n",
       "41050  2016  Alabama       AL  Autauga  1001  President  Hillary Clinton   \n",
       "41051  2016  Alabama       AL  Autauga  1001  President     Donald Trump   \n",
       "41052  2016  Alabama       AL  Autauga  1001  President            Other   \n",
       "41053  2016  Alabama       AL  Baldwin  1003  President  Hillary Clinton   \n",
       "41054  2016  Alabama       AL  Baldwin  1003  President     Donald Trump   \n",
       "\n",
       "            party  candidatevotes  totalvotes   version area_fips  \n",
       "41050    democrat          5936.0       24973  20190722      1001  \n",
       "41051  republican         18172.0       24973  20190722      1001  \n",
       "41052         NaN           865.0       24973  20190722      1001  \n",
       "41053    democrat         18458.0       95215  20190722      1003  \n",
       "41054  republican         72883.0       95215  20190722      1003  "
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "def add_zero(x):\n",
    "    if len(x) == 4:\n",
    "        x = '0' + x\n",
    "    else:\n",
    "        x = x \n",
    "    return x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "df[\"area_fips\"] = df.area_fips.apply(add_zero)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "df[\"vote_share\"] = df.candidatevotes / df.totalvotes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_trump = df[df.candidate == \"Donald Trump\"]\n",
    "# Just look at the trump share"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>year</th>\n",
       "      <th>state</th>\n",
       "      <th>state_po</th>\n",
       "      <th>county</th>\n",
       "      <th>FIPS</th>\n",
       "      <th>office</th>\n",
       "      <th>candidate</th>\n",
       "      <th>party</th>\n",
       "      <th>candidatevotes</th>\n",
       "      <th>totalvotes</th>\n",
       "      <th>version</th>\n",
       "      <th>area_fips</th>\n",
       "      <th>vote_share</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>41051</td>\n",
       "      <td>2016</td>\n",
       "      <td>Alabama</td>\n",
       "      <td>AL</td>\n",
       "      <td>Autauga</td>\n",
       "      <td>1001</td>\n",
       "      <td>President</td>\n",
       "      <td>Donald Trump</td>\n",
       "      <td>republican</td>\n",
       "      <td>18172.0</td>\n",
       "      <td>24973</td>\n",
       "      <td>20190722</td>\n",
       "      <td>01001</td>\n",
       "      <td>0.727666</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>41054</td>\n",
       "      <td>2016</td>\n",
       "      <td>Alabama</td>\n",
       "      <td>AL</td>\n",
       "      <td>Baldwin</td>\n",
       "      <td>1003</td>\n",
       "      <td>President</td>\n",
       "      <td>Donald Trump</td>\n",
       "      <td>republican</td>\n",
       "      <td>72883.0</td>\n",
       "      <td>95215</td>\n",
       "      <td>20190722</td>\n",
       "      <td>01003</td>\n",
       "      <td>0.765457</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>41057</td>\n",
       "      <td>2016</td>\n",
       "      <td>Alabama</td>\n",
       "      <td>AL</td>\n",
       "      <td>Barbour</td>\n",
       "      <td>1005</td>\n",
       "      <td>President</td>\n",
       "      <td>Donald Trump</td>\n",
       "      <td>republican</td>\n",
       "      <td>5454.0</td>\n",
       "      <td>10469</td>\n",
       "      <td>20190722</td>\n",
       "      <td>01005</td>\n",
       "      <td>0.520967</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>41060</td>\n",
       "      <td>2016</td>\n",
       "      <td>Alabama</td>\n",
       "      <td>AL</td>\n",
       "      <td>Bibb</td>\n",
       "      <td>1007</td>\n",
       "      <td>President</td>\n",
       "      <td>Donald Trump</td>\n",
       "      <td>republican</td>\n",
       "      <td>6738.0</td>\n",
       "      <td>8819</td>\n",
       "      <td>20190722</td>\n",
       "      <td>01007</td>\n",
       "      <td>0.764032</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>41063</td>\n",
       "      <td>2016</td>\n",
       "      <td>Alabama</td>\n",
       "      <td>AL</td>\n",
       "      <td>Blount</td>\n",
       "      <td>1009</td>\n",
       "      <td>President</td>\n",
       "      <td>Donald Trump</td>\n",
       "      <td>republican</td>\n",
       "      <td>22859.0</td>\n",
       "      <td>25588</td>\n",
       "      <td>20190722</td>\n",
       "      <td>01009</td>\n",
       "      <td>0.893348</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>50498</td>\n",
       "      <td>2016</td>\n",
       "      <td>Wyoming</td>\n",
       "      <td>WY</td>\n",
       "      <td>Sweetwater</td>\n",
       "      <td>56037</td>\n",
       "      <td>President</td>\n",
       "      <td>Donald Trump</td>\n",
       "      <td>republican</td>\n",
       "      <td>12154.0</td>\n",
       "      <td>17130</td>\n",
       "      <td>20190722</td>\n",
       "      <td>56037</td>\n",
       "      <td>0.709515</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>50501</td>\n",
       "      <td>2016</td>\n",
       "      <td>Wyoming</td>\n",
       "      <td>WY</td>\n",
       "      <td>Teton</td>\n",
       "      <td>56039</td>\n",
       "      <td>President</td>\n",
       "      <td>Donald Trump</td>\n",
       "      <td>republican</td>\n",
       "      <td>3921.0</td>\n",
       "      <td>12627</td>\n",
       "      <td>20190722</td>\n",
       "      <td>56039</td>\n",
       "      <td>0.310525</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>50504</td>\n",
       "      <td>2016</td>\n",
       "      <td>Wyoming</td>\n",
       "      <td>WY</td>\n",
       "      <td>Uinta</td>\n",
       "      <td>56041</td>\n",
       "      <td>President</td>\n",
       "      <td>Donald Trump</td>\n",
       "      <td>republican</td>\n",
       "      <td>6154.0</td>\n",
       "      <td>8470</td>\n",
       "      <td>20190722</td>\n",
       "      <td>56041</td>\n",
       "      <td>0.726564</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>50507</td>\n",
       "      <td>2016</td>\n",
       "      <td>Wyoming</td>\n",
       "      <td>WY</td>\n",
       "      <td>Washakie</td>\n",
       "      <td>56043</td>\n",
       "      <td>President</td>\n",
       "      <td>Donald Trump</td>\n",
       "      <td>republican</td>\n",
       "      <td>2911.0</td>\n",
       "      <td>3814</td>\n",
       "      <td>20190722</td>\n",
       "      <td>56043</td>\n",
       "      <td>0.763241</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>50510</td>\n",
       "      <td>2016</td>\n",
       "      <td>Wyoming</td>\n",
       "      <td>WY</td>\n",
       "      <td>Weston</td>\n",
       "      <td>56045</td>\n",
       "      <td>President</td>\n",
       "      <td>Donald Trump</td>\n",
       "      <td>republican</td>\n",
       "      <td>3033.0</td>\n",
       "      <td>3526</td>\n",
       "      <td>20190722</td>\n",
       "      <td>56045</td>\n",
       "      <td>0.860182</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>3154 rows × 13 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       year    state state_po      county   FIPS     office     candidate  \\\n",
       "41051  2016  Alabama       AL     Autauga   1001  President  Donald Trump   \n",
       "41054  2016  Alabama       AL     Baldwin   1003  President  Donald Trump   \n",
       "41057  2016  Alabama       AL     Barbour   1005  President  Donald Trump   \n",
       "41060  2016  Alabama       AL        Bibb   1007  President  Donald Trump   \n",
       "41063  2016  Alabama       AL      Blount   1009  President  Donald Trump   \n",
       "...     ...      ...      ...         ...    ...        ...           ...   \n",
       "50498  2016  Wyoming       WY  Sweetwater  56037  President  Donald Trump   \n",
       "50501  2016  Wyoming       WY       Teton  56039  President  Donald Trump   \n",
       "50504  2016  Wyoming       WY       Uinta  56041  President  Donald Trump   \n",
       "50507  2016  Wyoming       WY    Washakie  56043  President  Donald Trump   \n",
       "50510  2016  Wyoming       WY      Weston  56045  President  Donald Trump   \n",
       "\n",
       "            party  candidatevotes  totalvotes   version area_fips  vote_share  \n",
       "41051  republican         18172.0       24973  20190722     01001    0.727666  \n",
       "41054  republican         72883.0       95215  20190722     01003    0.765457  \n",
       "41057  republican          5454.0       10469  20190722     01005    0.520967  \n",
       "41060  republican          6738.0        8819  20190722     01007    0.764032  \n",
       "41063  republican         22859.0       25588  20190722     01009    0.893348  \n",
       "...           ...             ...         ...       ...       ...         ...  \n",
       "50498  republican         12154.0       17130  20190722     56037    0.709515  \n",
       "50501  republican          3921.0       12627  20190722     56039    0.310525  \n",
       "50504  republican          6154.0        8470  20190722     56041    0.726564  \n",
       "50507  republican          2911.0        3814  20190722     56043    0.763241  \n",
       "50510  republican          3033.0        3526  20190722     56045    0.860182  \n",
       "\n",
       "[3154 rows x 13 columns]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_trump"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This stuff below basically mimics what goes on in the mapping notebook. Grab the trade data, will look at the cross-section as of December 2018. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "file_path = os.getcwd()\n",
    "\n",
    "trade_data = pq.read_table(file_path + \"\\\\data\\\\total_trade_data.parquet\").to_pandas()\n",
    "\n",
    "trade_data[\"time\"] = pd.to_datetime(trade_data.time)\n",
    "\n",
    "trade_data.set_index([\"area_fips\", \"time\"],inplace = True)\n",
    "\n",
    "trade_data[\"tariff_change\"] = trade_data.groupby([\"area_fips\"]).tariff.diff(12)\n",
    "\n",
    "trade_data.sort_values([\"area_fips\", \"time\"], inplace = True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "tariff_df = trade_data.xs('2018-12-1', level=1).copy()\n",
    "\n",
    "idx = pd.IndexSlice\n",
    "\n",
    "tariff_df[\"china_trade_2017\"] = trade_data.loc[idx[:,\"2017\"],:].groupby([\"area_fips\"]).china_exp_pc.sum()\n",
    "# This will create an annual trade flow measure by summing up exports per capita over the all months\n",
    "# in 2017 (before the war started)\n",
    "\n",
    "tariff_df[\"fips_code\"] = tariff_df.index\n",
    "\n",
    "tariff_df[\"fips_code\"] = tariff_df[\"fips_code\"].astype(int)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>total_exp_pc</th>\n",
       "      <th>china_exp_pc</th>\n",
       "      <th>tariff</th>\n",
       "      <th>emplvl_2017</th>\n",
       "      <th>fips</th>\n",
       "      <th>total_employment</th>\n",
       "      <th>tariff_change</th>\n",
       "      <th>china_trade_2017</th>\n",
       "      <th>fips_code</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>area_fips</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>10001</td>\n",
       "      <td>524.630933</td>\n",
       "      <td>23.745690</td>\n",
       "      <td>2.426361</td>\n",
       "      <td>2843.0</td>\n",
       "      <td>10001</td>\n",
       "      <td>29514.0</td>\n",
       "      <td>1.357466</td>\n",
       "      <td>565.842217</td>\n",
       "      <td>10001</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>10003</td>\n",
       "      <td>499.745029</td>\n",
       "      <td>39.971812</td>\n",
       "      <td>0.456547</td>\n",
       "      <td>9072.0</td>\n",
       "      <td>10003</td>\n",
       "      <td>249775.0</td>\n",
       "      <td>0.245212</td>\n",
       "      <td>442.806468</td>\n",
       "      <td>10003</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>10005</td>\n",
       "      <td>920.169247</td>\n",
       "      <td>52.514965</td>\n",
       "      <td>3.579051</td>\n",
       "      <td>9358.0</td>\n",
       "      <td>10005</td>\n",
       "      <td>60389.0</td>\n",
       "      <td>1.942987</td>\n",
       "      <td>918.184061</td>\n",
       "      <td>10005</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1001</td>\n",
       "      <td>2032.472813</td>\n",
       "      <td>220.403712</td>\n",
       "      <td>2.185990</td>\n",
       "      <td>891.0</td>\n",
       "      <td>1001</td>\n",
       "      <td>6100.0</td>\n",
       "      <td>0.844549</td>\n",
       "      <td>2594.575079</td>\n",
       "      <td>1001</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1003</td>\n",
       "      <td>728.597752</td>\n",
       "      <td>55.161192</td>\n",
       "      <td>1.358526</td>\n",
       "      <td>4993.0</td>\n",
       "      <td>1003</td>\n",
       "      <td>58645.0</td>\n",
       "      <td>0.822081</td>\n",
       "      <td>953.941297</td>\n",
       "      <td>1003</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           total_exp_pc  china_exp_pc    tariff  emplvl_2017   fips  \\\n",
       "area_fips                                                             \n",
       "10001        524.630933     23.745690  2.426361       2843.0  10001   \n",
       "10003        499.745029     39.971812  0.456547       9072.0  10003   \n",
       "10005        920.169247     52.514965  3.579051       9358.0  10005   \n",
       "1001        2032.472813    220.403712  2.185990        891.0   1001   \n",
       "1003         728.597752     55.161192  1.358526       4993.0   1003   \n",
       "\n",
       "           total_employment  tariff_change  china_trade_2017  fips_code  \n",
       "area_fips                                                                \n",
       "10001               29514.0       1.357466        565.842217      10001  \n",
       "10003              249775.0       0.245212        442.806468      10003  \n",
       "10005               60389.0       1.942987        918.184061      10005  \n",
       "1001                 6100.0       0.844549       2594.575079       1001  \n",
       "1003                58645.0       0.822081        953.941297       1003  "
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tariff_df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "tariff_trump = tariff_df.merge(df_trump[[\"vote_share\", \"FIPS\"]], left_on = \"area_fips\", right_on = \"FIPS\",\n",
    "                              indicator = True, how = \"left\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\ProgramData\\Anaconda3\\lib\\site-packages\\pandas\\core\\series.py:853: RuntimeWarning: divide by zero encountered in log\n",
      "  result = getattr(ufunc, method)(*inputs, **kwargs)\n"
     ]
    }
   ],
   "source": [
    "tariff_trump[\"const\"] = 1\n",
    "\n",
    "tariff_trump[\"log_exp_china\"] = np.log(tariff_trump[\"china_trade_2017\"]).replace(-np.inf, np.nan)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here's how I will pose the question. Ok, what could China be doing? Obvious answer is, place the tariff the stuff we export the most from. We buy a lot of soybeans and pork, well no more...so put the tariff there. This is what this regression does below. Correlate the tariff change with exports in 2017..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                            WLS Regression Results                            \n",
      "==============================================================================\n",
      "Dep. Variable:          tariff_change   R-squared:                       0.378\n",
      "Model:                            WLS   Adj. R-squared:                  0.378\n",
      "Method:                 Least Squares   F-statistic:                     113.8\n",
      "Date:                Fri, 11 Oct 2019   Prob (F-statistic):           4.43e-26\n",
      "Time:                        07:33:53   Log-Likelihood:                -5169.8\n",
      "No. Observations:                2850   AIC:                         1.034e+04\n",
      "Df Residuals:                    2848   BIC:                         1.036e+04\n",
      "Df Model:                           1                                         \n",
      "Covariance Type:                  HC3                                         \n",
      "=================================================================================\n",
      "                    coef    std err          z      P>|z|      [0.025      0.975]\n",
      "---------------------------------------------------------------------------------\n",
      "const            -2.1930      0.291     -7.526      0.000      -2.764      -1.622\n",
      "log_exp_china     0.4636      0.043     10.668      0.000       0.378       0.549\n",
      "==============================================================================\n",
      "Omnibus:                     1649.999   Durbin-Watson:                   1.685\n",
      "Prob(Omnibus):                  0.000   Jarque-Bera (JB):            43012.214\n",
      "Skew:                           2.246   Prob(JB):                         0.00\n",
      "Kurtosis:                      21.494   Cond. No.                         46.9\n",
      "==============================================================================\n",
      "\n",
      "Warnings:\n",
      "[1] Standard Errors are heteroscedasticity robust (HC3)\n"
     ]
    }
   ],
   "source": [
    "all_vars = [\"const\", \"tariff_change\", 'vote_share', \"log_exp_china\", \"total_employment\"]\n",
    "\n",
    "dataset = tariff_trump[all_vars].dropna()\n",
    "\n",
    "exog_vars = [\"const\", \"log_exp_china\"]\n",
    "\n",
    "mod = sm.WLS(dataset.tariff_change, dataset[exog_vars], weights = dataset.total_employment)\n",
    "\n",
    "res = mod.fit(cov_type='HC3')\n",
    "\n",
    "print(res.summary())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Ok: The more a county was exposed to chinese exports, the harder it was hit. Second, this explains like 38 percent of the variation in change in the tariff.\n",
    "\n",
    "Now lets add President Trump's share of the vote..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                            WLS Regression Results                            \n",
      "==============================================================================\n",
      "Dep. Variable:          tariff_change   R-squared:                       0.407\n",
      "Model:                            WLS   Adj. R-squared:                  0.406\n",
      "Method:                 Least Squares   F-statistic:                     41.01\n",
      "Date:                Fri, 11 Oct 2019   Prob (F-statistic):           2.75e-18\n",
      "Time:                        07:33:53   Log-Likelihood:                -5103.3\n",
      "No. Observations:                2850   AIC:                         1.021e+04\n",
      "Df Residuals:                    2847   BIC:                         1.023e+04\n",
      "Df Model:                           2                                         \n",
      "Covariance Type:                  HC3                                         \n",
      "=================================================================================\n",
      "                    coef    std err          z      P>|z|      [0.025      0.975]\n",
      "---------------------------------------------------------------------------------\n",
      "const            -2.2303      0.353     -6.321      0.000      -2.922      -1.539\n",
      "vote_share        0.7953      0.141      5.639      0.000       0.519       1.072\n",
      "log_exp_china     0.4188      0.047      8.872      0.000       0.326       0.511\n",
      "==============================================================================\n",
      "Omnibus:                     1880.410   Durbin-Watson:                   1.701\n",
      "Prob(Omnibus):                  0.000   Jarque-Bera (JB):            51120.188\n",
      "Skew:                           2.707   Prob(JB):                         0.00\n",
      "Kurtosis:                      23.029   Cond. No.                         47.2\n",
      "==============================================================================\n",
      "\n",
      "Warnings:\n",
      "[1] Standard Errors are heteroscedasticity robust (HC3)\n"
     ]
    }
   ],
   "source": [
    "all_vars = [\"const\", \"tariff_change\", 'vote_share', \"log_exp_china\", \"total_employment\"]\n",
    "\n",
    "dataset = tariff_trump[all_vars].dropna()\n",
    "\n",
    "exog_vars = [\"const\", 'vote_share', \"log_exp_china\"]\n",
    "\n",
    "mod = sm.WLS(dataset.tariff_change, dataset[exog_vars], weights = dataset.total_employment)\n",
    "\n",
    "res = mod.fit(cov_type='HC3')\n",
    "\n",
    "print(res.summary())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "What do we see: Export coefficient did not change much. The vote share coefficient is positive meaning: places that had a larger share of the vote going towards President Trump, have a larger increase in their tariff. The other issue to notice as well is that explanatory power only increases by about 2-3 percent. Here is just the vote share."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                            WLS Regression Results                            \n",
      "==============================================================================\n",
      "Dep. Variable:          tariff_change   R-squared:                       0.132\n",
      "Model:                            WLS   Adj. R-squared:                  0.132\n",
      "Method:                 Least Squares   F-statistic:                     137.8\n",
      "Date:                Fri, 11 Oct 2019   Prob (F-statistic):           4.22e-31\n",
      "Time:                        07:33:53   Log-Likelihood:                -5645.1\n",
      "No. Observations:                2850   AIC:                         1.129e+04\n",
      "Df Residuals:                    2848   BIC:                         1.131e+04\n",
      "Df Model:                           1                                         \n",
      "Covariance Type:                  HC3                                         \n",
      "==============================================================================\n",
      "                 coef    std err          z      P>|z|      [0.025      0.975]\n",
      "------------------------------------------------------------------------------\n",
      "const          0.1957      0.067      2.929      0.003       0.065       0.327\n",
      "vote_share     1.6197      0.138     11.737      0.000       1.349       1.890\n",
      "==============================================================================\n",
      "Omnibus:                     1716.568   Durbin-Watson:                   1.562\n",
      "Prob(Omnibus):                  0.000   Jarque-Bera (JB):            35027.317\n",
      "Skew:                           2.467   Prob(JB):                         0.00\n",
      "Kurtosis:                      19.451   Cond. No.                         7.15\n",
      "==============================================================================\n",
      "\n",
      "Warnings:\n",
      "[1] Standard Errors are heteroscedasticity robust (HC3)\n"
     ]
    }
   ],
   "source": [
    "all_vars = [\"const\", \"tariff_change\", 'vote_share', \"log_exp_china\", \"total_employment\"]\n",
    "\n",
    "dataset = tariff_trump[all_vars].dropna()\n",
    "\n",
    "exog_vars = [\"const\", 'vote_share']\n",
    "\n",
    "mod = sm.WLS(dataset.tariff_change, dataset[exog_vars], weights = dataset.total_employment)\n",
    "\n",
    "res = mod.fit(cov_type='HC3')\n",
    "\n",
    "print(res.summary())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The coefficient is bigger, explanatory power looks larger. **Issue is that there is a correlation between places exporting a lot to china and the vote share for President Trump** This is the real puzzle (as many have pointed out) that it appears places are voting in direct conflict with their economic interest. The regression below confirms this observation."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                            WLS Regression Results                            \n",
      "==============================================================================\n",
      "Dep. Variable:             vote_share   R-squared:                       0.111\n",
      "Model:                            WLS   Adj. R-squared:                  0.111\n",
      "Method:                 Least Squares   F-statistic:                     17.79\n",
      "Date:                Fri, 11 Oct 2019   Prob (F-statistic):           2.54e-05\n",
      "Time:                        07:33:53   Log-Likelihood:                -1421.5\n",
      "No. Observations:                2850   AIC:                             2847.\n",
      "Df Residuals:                    2848   BIC:                             2859.\n",
      "Df Model:                           1                                         \n",
      "Covariance Type:                  HC3                                         \n",
      "=================================================================================\n",
      "                    coef    std err          z      P>|z|      [0.025      0.975]\n",
      "---------------------------------------------------------------------------------\n",
      "const             0.0468      0.091      0.512      0.609      -0.132       0.226\n",
      "log_exp_china     0.0563      0.013      4.218      0.000       0.030       0.083\n",
      "==============================================================================\n",
      "Omnibus:                     2646.762   Durbin-Watson:                   1.445\n",
      "Prob(Omnibus):                  0.000   Jarque-Bera (JB):           207905.477\n",
      "Skew:                          -4.149   Prob(JB):                         0.00\n",
      "Kurtosis:                      44.011   Cond. No.                         46.9\n",
      "==============================================================================\n",
      "\n",
      "Warnings:\n",
      "[1] Standard Errors are heteroscedasticity robust (HC3)\n"
     ]
    }
   ],
   "source": [
    "all_vars = [\"const\", \"tariff_change\", 'vote_share', \"log_exp_china\", \"total_employment\"]\n",
    "\n",
    "dataset = tariff_trump[all_vars].dropna()\n",
    "\n",
    "exog_vars = [\"const\",  \"log_exp_china\"]\n",
    "\n",
    "mod = sm.WLS(dataset.vote_share, dataset[exog_vars], weights = dataset.total_employment)\n",
    "\n",
    "res = mod.fit(cov_type='HC3')\n",
    "\n",
    "print(res.summary())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
